﻿using AutoMapper;
using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace GlennQuigmire.API.Application.Commands
{
    public class MaterialCreateHander : IRequestHandler<MaterialCreateCommand, Result>
    {

        private readonly IBaseRepositorys<Material> baseRepositorys;
        private readonly IMapper mapper;
        public MaterialCreateHander(IBaseRepositorys<Material> baseRepositorys, IMapper mapper)
        {
            this.baseRepositorys = baseRepositorys;
            this.mapper = mapper;
        }


        /// <summary>
        /// 添加物料信息
        /// </summary>
        /// <param name="request">添加物料信息请求对象</param>
        /// <param name="cancellationToken">允许取消异步任务</param>
        /// <returns>添加物料信息响应对象</returns>
        public Task<Result> Handle(MaterialCreateCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new Result();
                Material material = mapper.Map<Material>(request);
                material.CreateDate=DateTime.Now;
                result.Code = baseRepositorys.Add(material);
                return Task.FromResult(result);
            }
            catch (Exception ex)
            {

                throw;
            }
        }
    }
}
